<?php
class indexAction extends baseAction{
	
	function index(){		
		$items = array();
		$items1 = array();
		$items2 = array();
		
		$rs = D("ju_item");
		
		//大图开始
		$big_item1 = $rs->where("1")->order("sort desc")->limit("0,1")->select();
		$this->assign("big_item1",$big_item1[0]);

		$big_item2 = $rs->where("1")->order("sort desc")->limit("1,1")->select();
		$this->assign("big_item2",$big_item2[0]);

		$big_item3 = $rs->where("1")->order("sort desc")->limit("2,1")->select();
		$this->assign("big_item3",$big_item3[0]);
		//大图over
		
		$items = $rs->where("1")->order("sort desc")->limit("3,5")->select();
		$this->assign("items",$items);

		$items1 = $rs->where("1")->order("sort desc")->limit("8,5")->select();
		$this->assign("items1",$items1);

		$items2 = $rs->where("1")->order("sort desc")->limit("13,5")->select();
		$this->assign("items2",$items2);

		//groupItem start
		$groupItem = $rs->where("classId=7")->order("sort desc")->limit("3,5")->select();
		$this->assign("groupItem",$groupItem);

		
		//slide 
		$rs = D("ju_slide");
		$slide = $rs->where("id<6")->select();
		$this->assign("slide",$slide);
		//small slide
		$smallSlide = $rs->where("id>=6 and id<=10")->select();
		$this->assign("smallSlide",$smallSlide);
		//slide over
		
		//header 
		$header = $rs->where("id=11")->select();
		//print_r($header[0]);
		$this->assign("header",$header[0]);
		
		$this->display();
	}
	
	public function product(){
		
		$rs = D("ju_item");
		$item = $rs->where("id=".$_GET['id'])->find();

$app_key = C("APP_KEY");//"12587554";
$secret=C("APP_SECRET");;
	$timestamp=time()."000";
	$message = $secret.'app_key'.$app_key.'timestamp'.$timestamp.$secret;
	$mysign=strtoupper(hash_hmac("md5",$message,$secret));
	setcookie("timestamp",$timestamp);
	setcookie("sign",$mysign);
	$num_iids = $item['item_id'];
	$ch = curl_init();
	$url = "http://gw.api.taobao.com/widget/rest?callback=&_t_sys=args%3D4&app_key=".$app_key."&outer_code=3273&fields=num_iid%2Cclick_url%2Ciid%2Ccommission%2Ccommission_rate%2Ccommission_num%2Ccommission_volume%2Ccommission_rate&method=taobao.taobaoke.widget.items.convert&num_iids=".$num_iids."&partner_id=top-sdk-js-20120801&sign=".$mysign."&timestamp=".$timestamp;
	
	curl_setopt($ch, CURLOPT_REFERER, "http://tuan.wantujie.com");
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_FAILONERROR, true);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	
	$resp = curl_exec($ch);
	

	$obj=json_decode($resp);
	$obj = (array)$obj;
	$taobaoke_items = (array)$obj['taobaoke_items'];
	//print_r($taobaoke_items);
	$taobaoke_item = (array)$taobaoke_items['taobaoke_item'];
	$tao_item = (array)$taobaoke_item[0];
	
	$item['click_url'] = $tao_item['click_url'];

		
		
		
		$this->assign("product",$item);
		//print_r($item[0]);
		//click+1
		$rs->where("id=".$_GET['id'])->setField(array("click"=>"click+1"));
		
		
		//
		
		$waterFall = $rs->where(" id>8 ")->order(" rand() ")->limit("0,30")->select();
		$this->assign('waterFall',$waterFall);
		
		//ad
		$rs = D("ju_slide");
		$ad = $rs->where("id=11")->select();
		$this->assign("ad",$ad[0]);
		//print_r($ad['picUrl']);
		//end of ad
		$this->display();
	}
	
	public function categray(){
		$rs = D("ju_item");
		//$pager = new pager();
		//iewPage($totalcount,$totalpage,$pagenum,$pagesize,$param,$url)
		//pagenum 但前页面；
		$pageSize = 24;
		$page = $_GET['page'];
		if($page<1)
			$page = 1;
		
		$limit = ($page-1) * $pageSize .",". $pageSize;
		
		//echo $sql;
		$cla = D("ju_class");
		$claType = $cla->where("id=".$_GET['classId'])->select();
		if($claType[0]['p_id']==0){
			$subType = $cla->where("p_id=".$claType[0]['id'])->select();//取大类的所有小类；
			$subClassId = array();
			foreach($subType as $k=>$v){
				$subClassId[] =  $v['id'];
			}
			
			$classId = implode(",",$subClassId);
			if($classId==''){
				$classId = $_GET['classId'];
			}
		}else{
			$classId = $_GET['classId'];
		}
		
		$totalNums = $rs->where("classId in(".$classId.")")->count("id");
		
		$items = $rs->where("classId in(".$classId.")")->limit($limit)->select();
		//echo $rs->getLastSql();
		
		
		$returnItems = array();
		for($i=0;$i<12;$i++){
			$returnItems[] = $items[$i]; 
		}
		$this->assign("items",$items);

		//print_r($totalNums);
		
		//echo $totalNums/24;
//		$p = $pager->ViewPage($totalNums,$totalNums/24,$page,24,"a=categray&classId=".$_GET['classId'],"/index.php");
		
		$cla = D("ju_class");
		$classType = $cla->where("id=".$_GET['classId'])->select();
		if($classType[0]['p_id']==0){
			$subClass = $cla->where("p_id=".$_GET['classId'])->select();
		}else{
		
			$subClass = $cla->where("p_id=".$classType[0]['p_id'])->select();
		}
		
		
		$this->assign("subClass",$subClass);

		$this->assign("currentPage",$page);
		
		$this->assign("page",$p);
		
		$this->display();
	}
	public function test(){
		

		$this->display();
	}
}
?>